<template>
  <div class="test-question">
    <div style="height: 40px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>测试题库</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" :model="formData" :rules="rules" ref="formData">
      <el-form-item label="测试分类" prop="testCategoryId">
        <el-select
          v-model="formData.testCategoryId"
          placeholder="测试分类"
          autocomplete="off"
          :clearable="true"
          :popper-append-to-body="false"
          style="width: 150px"
        >
          <el-option
            v-for="item in testCategoryArray"
            :key="item.id"
            :label="item.testName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="query">查询</el-button>
        <el-button type="info" @click="clear">清空</el-button>
      </el-form-item>
      <span
        style="float: right; margin-right: 28px; margin-top: 20px"
        @click="$router.push('/exam')"
        ><el-link type="info"
          ><i class="el-icon-view"></i>查看考试记录</el-link
        ></span
      >
    </el-form>
    <div
      class="test-table"
      v-loading="tableLoading"
      element-loading-text="拼命加载中"
    >
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%; font-size: 17px">
        <el-table-column prop="examName" label="试卷名称" align="center">
        </el-table-column>
        <el-table-column
          prop="testCategoryName"
          label="测试类型"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="duration" label="测试时长" align="center">
        </el-table-column>
        <el-table-column prop="score" label="试卷分值" align="center">
        </el-table-column>
        <el-table-column prop="count" label="试题数量" align="center">
        </el-table-column>
        <el-table-column label="发布时间" align="center">
          <template slot-scope="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="mini"
              v-if="scope.row.count !== 0"
              @click="
                toTest(
                  scope.row.id,
                  scope.row.duration,
                  scope.row.testCategoryId,
                  scope.row.examName
                )
              "
              >前往考试</el-button
            >
            <el-button type="danger" size="mini" v-else disabled
              >等待开放</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-show="tableData.length > 0"
        layout="prev, pager, next"
        :current-page="currentPage"
        @current-change="handleCurrentChange"
        :total="total"
        :page-count="7"
        style="text-align: right; margin-top: 20px"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getTestCategoryAll, getTestExamAll } from "@/api/client/testExam";
import { MessageBox } from "element-ui";

export default {
  name: "TestQuestionView",
  data() {
    return {
      // 测试分类
      testCategoryArray: [],
      formData: {
        testCategoryId: "",
      },
      rules: {
        testCategoryId: [
          { required: true, message: "请选择测试分类", trigger: "blur" },
        ],
      },
      page: 1,
      pageSize: 10,
      total: 0,
      currentPage: 1,
      tableData: [],
      tableLoading: false,
    };
  },
  created() {
    this.getAll();
    this.getDataList();
  },
  methods: {
    // 获取课程类型
    async getAll() {
      const res = await getTestCategoryAll();
      this.testCategoryArray = res.data;
    },
    // 分页查询
    query() {
      this.$refs.formData.validate(async (v) => {
        if (v) {
          this.page = 1;
          this.getDataList();
        }
      });
    },
    // 清空
    clear() {
      this.page = 1;
      this.currentPage = 1;
      this.$refs.formData.resetFields();
      this.getDataList();
    },
    // 分页查询
    handleCurrentChange(page) {
      this.page = page;
      this.currentPage = page;
      this.getDataList();
    },
    // 获取数据
    async getDataList() {
      try {
        this.tableLoading = true;
        const res = await getTestExamAll({
          ...this.formData,
          page: this.page,
          pageSize: this.pageSize,
        });
        this.tableData = res.data.records;
        this.total = res.data.total;
      } finally {
        this.tableLoading = false;
      }
    },
    // 时间格式判断
    padZero(num) {
      return num < 10 ? "0" + num : num;
    },
    // 时间格式化
    formatDate(timeArray) {
      if (!timeArray) return "暂无时间";
      let year = timeArray[0];
      let month = this.padZero(timeArray[1]);
      let day = this.padZero(timeArray[2]);
      let hour = this.padZero(timeArray[3]);
      let minute = this.padZero(timeArray[4]);
      let second = this.padZero(timeArray[5]) || "00";
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
    // 前往考试
    toTest(id, time, examName, testCategoryId) {
      MessageBox.confirm("确定要参加考试吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$router.push(
          `/testExam/${id}/${time}/${examName}/${testCategoryId}`
        );
      });
    },
  },
};
</script>

<style lang="less" scoped>
.test-question {
  width: 100%;
  height: 100%;
  background-color: #fff;
  padding: 10px 60px;
  .test-table {
    width: 96%;
    height: 100%;
    margin: 0 auto;
    padding: 20px;
    box-shadow: 1px 1px 8px rgba(0, 0, 0, 0.15);
    transition: 0.2s;
    &:hover {
      box-shadow: 1px 1px 12px rgba(0, 0, 0, 0.2);
    }
  }
}
// 解决下拉框显示不完整问题
/deep/.el-select-dropdown__wrap.el-scrollbar__wrap {
  margin-bottom: 0 !important;
}
</style>